extern	gdt_ptr
extern	idt_ptr
extern	cstart
extern	clock_handler
extern  key_handler
extern  send1 ;引入C语言send1过程
extern	receive1;引入C语言receive1过程
extern	handler
extern	exception_handler
extern	cur
extern	t_tss
global	restart
extern	main
global	message
global	divide_error
global	single_step_exception
global	nmi
global	breakpoint_exception
global	overflow
global	bounds_check
global	inval_opcode
global	copr_not_available
global	double_fault
global	copr_seg_overrun
global	inval_tss
global	segment_not_present
global	general_protection
global	stack_exception
global	page_fault
global	undefine
global	copr_error
global	hwint0
global 	hwint1
global	hwint2
global	hwint3
global	hwint4
global	hwint5
global	hwint6
global 	hwint7
global	hwint8
global	hwint9
global	hwint10
global	hwint11
global	hwint12
global	hwint13
global	hwint14
global	hwint15
global	send
global	receive
global	StackTop
global	k_reenter
[section .data]
k_reenter	dd	0	
tmp_Stack	dd	0
spid		dd	0
dpid		dd	0
msg			dd	0  ;消息结构的地址
[section .bss]
StackSpace	resb	2*1024
StackTop:
[section .text]
[bits 32]
global _start
_start:
	cli
	mov	ax,24
	mov	gs,ax
	mov	esp,StackTop
	sgdt	[gdt_ptr]
	sidt	[idt_ptr]
	call	main
	lgdt	[gdt_ptr]	
	lidt	[idt_ptr]
	jmp	08h:init
init:
	xor	eax,eax
	mov	ax,32
	ltr	ax
	call	cstart
	sti
	nop
divide_error:
	push	0xFFFFFFFF
	push	0
	jmp	exception
single_step_exception:
	push	0xFFFFFFFF
	push	1
	jmp	exception
nmi:
	push	0xFFFFFFFF
	push	2
	jmp	exception
breakpoint_exception:
	push	0xFFFFFFFF
	push	3
	jmp	exception
overflow:
	push	0xFFFFFFFF
	push	4
	jmp	exception
bounds_check:
	push	0xFFFFFFFF
	push	5
	jmp	exception
inval_opcode:
	push	0xFFFFFFFF
	push	6
	jmp	exception
copr_not_available:
	push	0xFFFFFFFF
	push	7
	jmp	exception
double_fault:
	push	0xFFFFFFFF
	push	8
	jmp	exception
copr_seg_overrun:
	push	0xFFFFFFFF
	push	9
	jmp	exception
inval_tss:
	push	0xFFFFFFFF
	push	10
	jmp	exception
segment_not_present:
	push	0xFFFFFFFF
	push	11
	jmp	exception
stack_exception:
	push	0xFFFFFFFF
	push	12
	jmp	exception
general_protection:
	push	0xFFFFFFFF
	push	13
	jmp	exception
page_fault:
	push	0xFFFFFFFF
	push	14
	jmp	exception
undefine:
	push	0xFFFFFFFF
	push	15
	jmp	exception
copr_error:
	push	0xFFFFFFFF
	push	16
	jmp	exception
exception:
	call	exception_handler
	add	esp,4*2
	jmp	$

;this is for clock interrupt 
hwint0:
	pushad
	push	gs
	push	fs
	push	es
	push	ds
	mov	ax,ss
	mov	ds,ax
	mov	es,ax
	
	mov	al,0ffh
	out	21h,al
	
	inc	dword[k_reenter]
	cmp	dword[k_reenter],1
	jnz	re_enter
	cli
	mov	esp,StackTop
	call	clock_handler
restart:
	mov	esp,[cur]
	lldt	[esp+68]
	lea	eax,[esp+68]
	mov	[t_tss+4],eax
	dec	dword[k_reenter]
	
	mov	al,20h
	out	20h,al
	mov	al,0fch
	out	21h,al
	pop	ds
	pop	es
	pop	fs
	pop	gs
	popad
	sti
	iret
re_enter:
	dec	dword[k_reenter]
	pop	ds
	pop	es
	pop	fs
	pop	gs
	popad
	iret
;this is keyboard interrupt
hwint1:
;	push	gs
;	push	fs
;	push	es
;	push	ds
;	mov	ax,ss
;	mov	ds,ax
;	mov	es,ax	
	cli	
	pushad
	push	gs
	push	fs
	push	es
	push	ds

	mov	ax,ss
	mov	ds,ax
	mov	es,ax
	mov	dword[tmp_Stack],esp
	xor 	al,al
	mov	al,0ffh
	out	21h,al
	
	mov	esp,StackTop
	call key_handler
		
	mov	al,20h
	out	20h,al
	mov	al,0fch
	out	21h,al
	
	mov	esp,dword[tmp_Stack]
;	popad
;	sti
	pop	ds
	pop	es
	pop	fs
	pop	gs
	popad
	sti
	iret
hwint2:
	call handler
	iret
hwint3:
	call handler
	iret
hwint4:
	call handler
	iret
hwint5:
	call handler
	iret
hwint6:
	call handler
	iret
hwint7:
	call handler
	iret
hwint8:
	call handler
	iret
hwint9:
	call handler
	iret
hwint10:
	call handler
	iret
hwint11:
	call handler
	iret
hwint12:
	call handler
	iret
hwint13:
	call handler
	iret
hwint14:
	call handler
	iret
hwint15:
	call handler
	iret
send:
	cli
	mov	dword[msg],eax ;将寄存器中的地址放在msg变量中
;	mov	dword[dpid],ebx
	pushad
	push	gs
	push	fs
	push	es
	push	ds

	mov	ax,ss
	mov	ds,ax
	mov	dword[tmp_Stack],esp
	
	mov	esp,StackTop
	push	dword[msg];将消息地址压入栈
	push	dword[cur];将当前的运行的进程地址压入栈 
;	jmp	$
	call	send1 ;调用消息转发机制
	
	mov	esp,dword[tmp_Stack]
	pop	ds
	pop	es
	pop	fs
	pop	gs
	popad
	sti
	iret
receive:
	cli
	mov	dword[msg],eax ;将寄存器中的地址放在msg变量中
;	mov	dword[dpid],ebx
	pushad
	push	gs
	push	fs
	push	es
	push	ds

	mov	ax,ss
	mov	ds,ax
	mov	dword[tmp_Stack],esp
	
	mov	esp,StackTop
	push	dword[msg];将消息地址压入栈
	push	dword[cur];将当前的运行的进程地址压入栈 
;	jmp	$
	call	receive1 ;调用消息转发机制
	
	mov	esp,dword[tmp_Stack]
	pop	ds
	pop	es
	pop	fs
	pop	gs
	popad
	sti
	iret
